package applet.login.components;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;

import applet.login.CMSLoginView;

public class LoginPane extends JPanel
{
	private static final long serialVersionUID = 2079810179996661498L;

	private CMSLoginView cmsLoginView;

	private JTextField username;
	private JPasswordField password;
	
	public LoginPane(CMSLoginView cmsLoginView)
	{
		super(new BorderLayout());
		this.cmsLoginView = cmsLoginView;
		
		setBorder(new BevelBorder(BevelBorder.RAISED));
		
		JPanel titlePane = new JPanel(new BorderLayout());
		titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
			JLabel title = new JLabel("Please Log In:");
			title.setFont(title.getFont().deriveFont(Font.BOLD));
			titlePane.add(title, BorderLayout.WEST);
		add(titlePane, BorderLayout.NORTH);
		
		JPanel subPane = new JPanel(new GridLayout(3, 2));
		subPane.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), 
											 new EmptyBorder(20, 20, 20, 20)));
			JPanel layoutHelper = new JPanel(new BorderLayout());
				layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
					JLabel label = new JLabel("Name:");
				layoutHelper.add(label, BorderLayout.EAST);
			subPane.add(layoutHelper);
			
			layoutHelper = new JPanel(new BorderLayout());
				layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
					username = new JTextField(15);
				layoutHelper.add(username, BorderLayout.WEST);
			subPane.add(layoutHelper);
			
			layoutHelper = new JPanel(new BorderLayout());
				layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
					label = new JLabel("Password:");
				layoutHelper.add(label, BorderLayout.EAST);
			subPane.add(layoutHelper);
			
			layoutHelper = new JPanel(new BorderLayout());
				layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
					password = new JPasswordField(15);
				layoutHelper.add(password, BorderLayout.WEST);
			subPane.add(layoutHelper);
			
			JButton loginButton = new JButton("Login");
			loginButton.addActionListener(new LoginListener());
			subPane.add(loginButton);
			
			JButton registerButton = new JButton("Register");
			registerButton.addActionListener(new RegisterListener());
			subPane.add(registerButton);
			
		add(subPane, BorderLayout.CENTER);
	}
	
	public void reset()
	{
		username.setText("");
		password.setText("");
	}
	
	private class LoginListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			String uName = username.getText();
			String pWord = new String(password.getPassword());
			cmsLoginView.attemptLogin(uName, pWord);
		} }
	
	private class RegisterListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			
			String uName = username.getText();
			String pWord = new String(password.getPassword());
			
			if(uName.length() > 0 && pWord.length() > 0)
			{
				String[] options = new String[] { "Cancel", "Register Account" };
				int n = JOptionPane.showOptionDialog(CMSRes.frame(),
						"Are you sure you wish to create a new account?",
					    "Proceed with Registration?", 
					    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
					    null, options, options[0]);
				if(n == 1)
					cmsLoginView.attemptRegister(uName, pWord);
			}
			else
				JOptionPane.showMessageDialog(CMSRes.frame(), 
						"You must specify both a name and password!",
						"Registration Failed",
						JOptionPane.ERROR_MESSAGE);
		} }
}